package com.ssi.myshop.ui;

import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;

import com.ssi.myshop.Presenter.impl.ILoginPresenterImpl;
import com.ssi.myshop.Presenter.impl.IRegisterPresentImpl;
import com.ssi.myshop.R;
import com.ssi.myshop.base.BaseActivity;
import com.ssi.myshop.bean.LoginBean;
import com.ssi.myshop.utils.LoadingUtils;
import com.ssi.myshop.utils.PresenterManager;
import com.ssi.myshop.view.ILoginCallBack;
import com.ssi.myshop.view.IRegisterCallBack;

import butterknife.BindView;

public class WelcomActivity extends BaseActivity implements View.OnClickListener, IRegisterCallBack, ILoginCallBack {
    private static final String TAG = "WelcomActivity";

    @BindView(R.id.et_name)
    EditText mName;

    @BindView(R.id.et_pwd)
    EditText mPwd;

    @BindView(R.id.btn_login)
    Button mLogin;

    @BindView(R.id.btn_register)
    Button mRegister;
    private IRegisterPresentImpl iRegisterPresent;
    private AlertDialog dialog;
    private ILoginPresenterImpl iLoginPresenter;
    private SharedPreferences.Editor mEditor;
    private SharedPreferences mMysp;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_welcom;
    }

    @Override
    protected void initView() {
        mMysp = getSharedPreferences("mysp", MODE_PRIVATE);
        mEditor = mMysp.edit();
    }

    @Override
    protected void initPresent() {
        iRegisterPresent = PresenterManager.getInstance().getIRegisterPresent();
        iRegisterPresent.registerViewCallBack(this);
        iLoginPresenter = PresenterManager.getInstance().getLoginPresenter();
        iLoginPresenter.registerViewCallBack(this);

    }

    @Override
    protected void initEvent() {
        mRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                iRegisterPresent.doRegister(mName.getText().toString().trim(), mPwd.getText().toString().trim());
            }
        });

        mLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                iLoginPresenter.doLogin(mName.getText().toString().trim(), mPwd.getText().toString().trim());
            }
        });

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        iRegisterPresent.unregisterViewCallBack(this);
        iLoginPresenter.unregisterViewCallBack(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_register:

                break;
        }
    }

    @Override
    public void onRegisterSuccess() {
        Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
        LoadingUtils.dismissLoadingDialog();
    }

    @Override
    public void onLoginSuccess(LoginBean bean) {
        Intent intent = new Intent();
        intent.setClass(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
        String token = mMysp.getString("token", "");
        if (token == null || token == "" || !token.equals(bean.getToken())) {
            mEditor.putString("token", bean.getToken());
            mEditor.commit();
        }
    }

    @Override
    public void onError(String s) {
        Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
        LoadingUtils.dismissLoadingDialog();
    }

    @Override
    public void onLoading() {
        LoadingUtils.loading(this, "正在中....");
        LoadingUtils.dismissLoadingDialog();
    }

    @Override
    public void onEmpty() {

    }

    @Override
    public void onSuccess() {

    }
}
